package com.itboye.lottery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.bylibrary.util.TimeUtils;
import com.itboye.bylibrary.widget.glide.GlideUtil;
import com.itboye.lottery.R;
import com.itboye.lottery.bean.BetBean;
import com.itboye.lottery.fragment.home.football.TouZhuFragment;

import java.util.List;

import static com.itboye.bylibrary.util.ScreenUtil.dip2px;

/**
 * Created by wu on 2018/1/9.
 */

public class TouZhuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<BetBean> list;
    //    private int type;
    TouZhuFragment touZhuFragment;
    private int type2;

    public TouZhuAdapter(TouZhuFragment touZhuFragment, List<BetBean> list) {
        this.list = list;
        this.touZhuFragment = touZhuFragment;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(TouZhuAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setType2(int type2) {
        this.type2 = type2;
    }

//    public void setType(int type) {
//        this.type = type;
//    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_touzhu, parent, false);
        return new ViewHolder(v);
    }

    /**
     * 我的推荐中和我的跟单type2==1或者2时
     * 未支付：显示支付+ 截止时间
     * 已支付：
     * 已开奖（包含已中奖、未中奖）：显示收入详情
     * 未开奖：显示立即跟单
     * 已关闭：显示灰色的取支付
     * 投注记录：type=0
     * 未支付：显示支付+ 截止时间
     * 已开奖、未开奖：显示继续投注
     * 已关闭：显示支付（灰色）
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        BetBean betBean = list.get(position);
        GlideUtil.display(context, list.get(position).getLogo(), viewHolder.iv_item_bet_logo, R.mipmap.lottery_circle);
        viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.GONE);
        viewHolder.iv_item_touzhu_pay.setTag(position);
        viewHolder.iv_item_touzhu_extra.setText("");
        viewHolder.iv_item_touzhu_pay.setOnClickListener(null);
        viewHolder.iv_item_touzhu_pay.setClickable(false);
        viewHolder.iv_item_touzhu_goon.setOnClickListener(null);
        viewHolder.iv_item_touzhu_pay.setBackgroundResource((R.drawable.rounded_bg_red_border_none));
        viewHolder.iv_item_touzhu_pay.setPadding(dip2px(context, 6), dip2px(context, 6), dip2px(context, 6), dip2px(context, 6));
        viewHolder.iv_item_touzhu_goon.setVisibility(View.GONE);
        switch (type2) {//0.投注记录  1.我的跟单  2.我的推荐
            case 0:
                if (betBean.getIs_prize() == 1) {
                    viewHolder.tv_item_bet_status.setText(Html.fromHtml("已中奖<font color='red'>" + betBean.getPrize_money() + "</font>元"));
                } else {
                    viewHolder.tv_item_bet_status.setText(betBean.getStatus() + "");
                }
                viewHolder.tv_item_touzhu_time.setText(list.get(position).getTime());
                viewHolder.tv_item_touzhu_money.setText("投注" + list.get(position).getMoney() + "元");
                switch (list.get(position).getPay_status()) {
                    case 0:
                        //未支付
                        viewHolder.iv_item_touzhu_extra.setText("截止" + list.get(position).getEnd_time() + "交易自动关闭");
                        viewHolder.iv_item_touzhu_pay.setVisibility(View.VISIBLE);
                        viewHolder.iv_item_touzhu_pay.setText("支   付");
//                        viewHolder.tv_item_bet_status.setText("未支付");
                        viewHolder.iv_item_touzhu_pay.setOnClickListener(touZhuFragment);
                        break;
                    case 1:
                        //已支付
//                        if (list.get(position).getIs_mail() == 0) {
//                            //未邮寄
//                            viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.VISIBLE);
//                        } else {
//                            //已邮寄
//                            viewHolder.iv_item_touzhu_extra.setText("邮寄申请已提交，请至投注详情查看进度" + list.get(position).getEnd_time() + "交易自动关闭");
//                            viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.GONE);
//                        }
                        viewHolder.iv_item_touzhu_pay.setVisibility(View.GONE);
//                        if (list.get(position).getIs_prize() == 1) {
//                            viewHolder.tv_item_bet_status.setText(Html.fromHtml("已中奖<font color='red'>" + list.get(position).getPrize_money() + "元</font>"));
//                        } else {
//                            viewHolder.tv_item_bet_status.setText("待开奖");
//                        }
                        viewHolder.iv_item_touzhu_goon.setTag(R.id.tag_1, betBean.getGamecate());
                        viewHolder.iv_item_touzhu_goon.setVisibility(View.VISIBLE);
                        viewHolder.iv_item_touzhu_goon.setText("继续投注");
                        viewHolder.iv_item_touzhu_goon.setOnClickListener(touZhuFragment);
                        break;
                    case 2:
                        //交易关闭
//                        viewHolder.tv_item_bet_status.setText("已关闭");
                        viewHolder.iv_item_touzhu_pay.setVisibility(View.VISIBLE);
                        viewHolder.iv_item_touzhu_pay.setText("支    付");
                        viewHolder.iv_item_touzhu_pay.setBackgroundDrawable(touZhuFragment.getResources().getDrawable(R.drawable.rounded_bg_gray_border_none_corner_6));
                        viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.GONE);
                        break;
                }
                break;
            case 1:
            case 2:
                switch (list.get(position).getPay_status()) {
                    case 0:
                        //未支付
                        viewHolder.iv_item_touzhu_extra.setText("截止" + TimeUtils.getFormatTime(Long.parseLong(list.get(position).getEnd_time()) * 1000, "yyyy-MM-dd HH:mm") + "交易自动关闭");
                        viewHolder.iv_item_touzhu_pay.setVisibility(View.VISIBLE);
                        viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.GONE);
                        viewHolder.iv_item_touzhu_pay.setOnClickListener(touZhuFragment);
                        viewHolder.iv_item_touzhu_pay.setText("支    付");
                        viewHolder.tv_item_bet_status.setText(Html.fromHtml("未支付"));
                        break;
                    case 1:
                        //彩种类型0：数字彩 1：足彩 2篮彩
                        //betBean.getLottery_type()
                        if (betBean.getIs_result() != null) {
                            if (betBean.getIs_result().equals("1")) {//已开奖
                                viewHolder.iv_item_touzhu_extra.setVisibility(View.GONE);
                                if (betBean.getLottery_result().equals("1")) {//中奖
                                    viewHolder.tv_item_bet_status.setText(Html.fromHtml("<font color='red'>已中奖" + betBean.getWin_amount() + "元</font>"));
                                } else {
                                    viewHolder.tv_item_bet_status.setText(Html.fromHtml("未中奖"));
                                }
                                viewHolder.iv_item_touzhu_pay.setVisibility(View.VISIBLE);
                                viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.GONE);
                                viewHolder.iv_item_touzhu_pay.setText("收入详情");
                            } else {
                                viewHolder.iv_item_touzhu_extra.setVisibility(View.VISIBLE);
                                viewHolder.tv_item_bet_status.setText(Html.fromHtml("待开奖"));
                                viewHolder.iv_item_touzhu_pay.setVisibility(View.VISIBLE);
                                viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.GONE);
                                viewHolder.iv_item_touzhu_pay.setText("立即跟单");
                            }
                        }else{

                        }
                        break;
                    case 2:
                        //交易关闭
                        viewHolder.tv_item_bet_status.setText(Html.fromHtml("已关闭"));
                        viewHolder.iv_item_touzhu_pay.setVisibility(View.VISIBLE);
                        viewHolder.iv_item_touzhu_pay.setBackgroundDrawable(touZhuFragment.getResources().getDrawable(R.drawable.rounded_bg_gray_border_none_corner_6));
                        viewHolder.iv_item_touzhu_youji_caipiao.setVisibility(View.GONE);
                        viewHolder.iv_item_touzhu_pay.setText("支    付");
                        break;
                }
                viewHolder.tv_item_touzhu_time.setText(TimeUtils.getFormatTime(list.get(position).getCreate_time() * 1000, "yyyy-MM-dd"));
                viewHolder.tv_item_touzhu_money.setText("投注" + betBean.getTotal_amount() + "元");
                break;
//            case 2:
//                viewHolder.iv_item_touzhu_pay.setText("立即跟单");
//                viewHolder.iv_item_touzhu_goon.setText("收入详情");
//                break;
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView img, iv_item_bet_logo;
        public TextView name;
        public TextView tv_item_bet_status, tv_item_touzhu_money, tv_item_touzhu_time, iv_item_touzhu_extra;
        public TextView iv_item_touzhu_youji_caipiao, iv_item_touzhu_goon, iv_item_touzhu_pay;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            iv_item_bet_logo = itemView.findViewById(R.id.iv_item_bet_logo);
            tv_item_bet_status = itemView.findViewById(R.id.tv_item_bet_status);
            tv_item_touzhu_money = itemView.findViewById(R.id.tv_item_touzhu_money);
            tv_item_touzhu_time = itemView.findViewById(R.id.tv_item_touzhu_time);
            iv_item_touzhu_extra = itemView.findViewById(R.id.iv_item_touzhu_extra);

            iv_item_touzhu_youji_caipiao = itemView.findViewById(R.id.iv_item_touzhu_youji_caipiao);
            iv_item_touzhu_goon = itemView.findViewById(R.id.iv_item_touzhu_goon);
            iv_item_touzhu_pay = itemView.findViewById(R.id.iv_item_touzhu_pay);

//            iv_item_touzhu_youji_caipiao.setOnClickListener(this);
//            iv_item_touzhu_goon.setOnClickListener(this);
//            iv_item_touzhu_pay.setOnClickListener(this);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
